<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/8/3
 * Time: 13:04
 */
declare (strict_types=1);

namespace app\admin\controller;

use app\model\DetailModel;
use app\model\CategoryModel;
use app\model\OrderModel;
use app\model\UserModel;
use app\model\WareModel;
use think\facade\Request;
use think\facade\Validate;
use think\facade\View;

class Order
{
    public function index()
    {
        $orderList = OrderModel::select();

        foreach ($orderList as &$row){
        $user = UserModel::find($row['user_id']);
        $row['user_name'] = $user['user_name'];     //用户名称赋值给当前订单

        // $ware = WareModel::find($row['ware_id']);
        // $row['ware_name'] = $ware['ware_name'];     //商品名称赋值给当前订单
        }
        unset($row);
        View::assign('orderList',$orderList);
        return View::fetch();
    }

    public function add()
    {
        $orderList = OrderModel::select();
        return View::fetch('', ['orderList' => $orderList]);

    }
    public function addSave()
    {
        $params = Request::param();
        var_dump($params);
        $validate = Validate::rule([
            'user_id|用户id' =>'require|between:1,' . PHP_INT_MAX,
//            'user_table|桌号' => 'require|between:1,' . PHP_INT_MAX,
            'ware_id|订单id' => 'require|between:1,' . PHP_INT_MAX,
            'num|数量' => 'require|between:1,' . PHP_INT_MAX,
            'order_status|订单状态' => 'require|min:2|max:45',
            'order_price|应付金额' => 'require|between:1,' . PHP_INT_MAX
        ]);
        if (!$validate->check($params)) {
            echo $validate->getError();
            exit();
        }

        $params['add_time'] = time();
        $params['update_time'] = time();
        $result = OrderModel::create($params);

        return View::fetch('public/tips', ['result' => $result]);

    }

    public function detail()
    {
        $orderId = Request::param("order_id");
        $validate = Validate::rule([
            'order_id|订单id' => 'require|between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check(['order_id' => $orderId])) {
            echo $validate->getError();
            exit();
        }
        $order = OrderModel::find($orderId);
        if (!$order) {
            echo '该订单不存在';
            exit();
        }
        $ware = WareModel::find($order['ware_id']);
        $order['ware_name'] = $ware['ware_name'];
        $order['ware_price'] = $ware['ware_price'];
        $order['ware_image'] = $ware['ware_image'];
        $order['ware_type'] = $ware['ware_type'];

        $user = UserModel::find($order['user_id']);
        $order['user_name'] = $user['user_name'];     //用户名称赋值给当前订单
        $order['user_table'] = $user['user_table'];

        return View::fetch('',['order' => $order]);

    }
    public function delete()
    {
        $orderId = Request::param("order_id");
        $validate = Validate::rule([
            'order_id|订单id' => 'require|between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check(['order_id' => $orderId])) {
            echo $validate->getError();
            exit();
        }

        $result = OrderModel::destroy($orderId);
        return View::fetch('public/tips', ['result' => $result]);

    }
      public function bill()
    {
        $orderId = Request::param("order_id");
        $validate = Validate::rule([
            'order_id|订单id' => 'require|between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check(['order_id' => $orderId])) {
            echo $validate->getError();
            exit();
        }
        $order = OrderModel::find($orderId);
        if (!$order) {
            echo '该订单不存在';
            exit();
        }
        $detail = DetailModel::where('order_id',"=", $orderId)->select();

        foreach ($detail as &$item){
                $ware = WareModel::find($item['ware_id']);
                $item['ware_name'] = $ware['ware_name'];
                $item['ware_price'] = $ware['ware_price'];
            }
        unset($item);

        return View::fetch('',['order' => $order,'detail'=>$detail]);
    }
}